package com.cwj.课程设计;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;

/**
 * 交通查询系统类
 * 统筹几个页面类，实现功能的封装
 */
public class TrafficInquirySystem {
    //查询方式
    private static final String[] TABNAME = {"路径处理", "查询", "记录"};
    //查询方式对应得列表，加入其仅仅为了代码的可读性更高
    private static final int QueryIndex = 1;
    private static final int RecordIndex = 2;
    //主页面
    private JFrame mainFrame;
    //准备图界面
    private PrepareTable prepareTable;
    //查询界面
    private QueryPane queryPane;
    //记录界面
    private RecordPane recordPane;
    //选项页
    private JTabbedPane tabbedPane;
    //载入的图
    private Graph_Name_star graph;

    public TrafficInquirySystem() {
        this.prePane();
    }

    public void preGraph() {
        this.graph = null;
    }

    public void prePane() {
        this.mainFrame = new JFrame();
        this.prepareTable = new PrepareTable();
        this.queryPane = new QueryPane();
        this.recordPane = new RecordPane();
        //添加窗口监听器，实现数据存储
        this.mainFrame.addWindowListener(new WindowListener() {
            @Override
            public void windowOpened(WindowEvent e) {

            }

            @Override
            public void windowClosing(WindowEvent e) {
                prepareTable.bufferSave();
                try {
                    recordPane.saveLastRecord();
                } catch (IOException ioException) {
                    JOptionPane.showMessageDialog(mainFrame, "无法保存记录", "警告", JOptionPane.ERROR_MESSAGE);
                }
            }

            @Override
            public void windowClosed(WindowEvent e) {

            }

            @Override
            public void windowIconified(WindowEvent e) {

            }

            @Override
            public void windowDeiconified(WindowEvent e) {

            }

            @Override
            public void windowActivated(WindowEvent e) {

            }

            @Override
            public void windowDeactivated(WindowEvent e) {

            }
        });

        this.queryPane.setRecordPane(this.recordPane);

        this.tabbedPane = new JTabbedPane();
        this.tabbedPane.addTab(TrafficInquirySystem.TABNAME[0], this.prepareTable.getMainPanel());
        this.tabbedPane.addTab(TrafficInquirySystem.TABNAME[1], this.queryPane.getMainPane());

        this.tabbedPane.addTab(TrafficInquirySystem.TABNAME[2], this.recordPane.getMainPane());
        this.mainFrame.add(this.tabbedPane, BorderLayout.CENTER);
        /**添加选择页变动监听器，可以实现将建图页面的的图加载到查询页面中*/
        this.tabbedPane.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
//                prepareTable.bufferSave();
                int name = tabbedPane.getSelectedIndex();
                if (name == QueryIndex&&prepareTable.isModify()) {
                    prepareTable.setModify(false);
                    Graph_Name_star gg = null;
                    queryPane.setGraph(gg = prepareTable.toGraph());
                    if (gg == null) {
                        queryPane.clearPane();
                        return;
                    }
                    queryPane.preGraph();
                } else if (name == TrafficInquirySystem.RecordIndex) {
                    recordPane.reDrawTable();
                }

            }
        });
        this.mainFrame.pack();//页面尺度自动变化
        this.mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.mainFrame.setVisible(true);
    }

    public static void main(String[] args) {
        TrafficInquirySystem system = new TrafficInquirySystem();
    }
}